package org.jeecg.modules.hc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Joiner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.mapping.Join;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.hc.entity.HcSiteAddressDetails;
import org.jeecg.modules.hc.service.IHcSiteAddressDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 站点地址明细
 * @Author: jeecg-boot
 * @Date: 2022-12-13
 * @Version: V1.0
 */
@Api(tags = "站点地址明细")
@RestController
@RequestMapping("/hc/hcSiteAddressDetails")
@Slf4j
public class HcSiteAddressDetailsController extends JeecgController<HcSiteAddressDetails, IHcSiteAddressDetailsService> {
    @Autowired
    private IHcSiteAddressDetailsService hcSiteAddressDetailsService;


    @ApiOperation("根据大厦id获取下单地址列表")
    @GetMapping("/getListByAddressId")
    public Result<?> getListByAddressId(@RequestParam(name = "addressId")String addressId,@RequestParam(name = "merchantId") String merchantId) {
        return Result.ok(hcSiteAddressDetailsService.getListByAddressId(addressId,merchantId));
    }

    @ApiOperation("修改大厦取餐地址展示情况")
    @PostMapping("/editAddressShowState")
    public Result<?> editAddressShowState(@RequestBody HcSiteAddressDetails param) {
        //根据id 获取大厦信息
        HcSiteAddressDetails hcSiteAddressDetails = hcSiteAddressDetailsService.getById(param.getId());
        if (null == hcSiteAddressDetails ){
            throw new JeecgBootException("未找到大厦信息");
        }
        String jsonSupplyAddress = hcSiteAddressDetails.getSupplyAddress();
        if (StringUtils.isNotBlank(jsonSupplyAddress)){
            JSONArray jsonArray = JSON.parseArray(jsonSupplyAddress);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String code = jsonObject.getString("code");
                //将id塞到json里面
                String supplyMerchantIds = jsonObject.getString("supplyMerchantIds");
                if (code.equals(param.getSupplyAddressCode())){
                    if (param.getIsShowAddress().equals("true")){//开启。需要将id去除
                        List<String> merchantIds = new ArrayList<>(Arrays.asList(supplyMerchantIds.split(",")));
                        List<String> merchantIdsToRemove = new ArrayList<>();

                        if (!merchantIdsToRemove.isEmpty()) {
                            merchantIds.removeAll(merchantIdsToRemove);
                            if (CollectionUtils.isNotEmpty(merchantIds)) {
                                String merchantIdString = Joiner.on(",").join(merchantIds);
                                jsonObject.put("supplyMerchantIds", merchantIdString);
                            } else {
                                jsonObject.put("supplyMerchantIds", "");
                            }
                        }
                    }else {//关闭，将id加进去
                    }
                }
            }
            hcSiteAddressDetails.setSupplyAddress(jsonArray.toJSONString());
            hcSiteAddressDetailsService.updateById(hcSiteAddressDetails);
        }
        return Result.ok("修改成功");
    }


    @ApiOperation("获取最近地址")
    @PostMapping("/getRecentAddress")
    public Result<?> getRecentAddress(@RequestBody HcSiteAddressDetails param) {
        return Result.ok(hcSiteAddressDetailsService.getRecentAddress(param));
    }

    /**
     * 返回下单页面中，用户可以选着的大厦列表；该请求无传参，
     * @return
     */
    @ApiOperation("获取地址列表")
    @GetMapping("/getAddressList")
    public Result<?> getAddressList() {
        return Result.ok(hcSiteAddressDetailsService.list(new LambdaQueryWrapper<HcSiteAddressDetails>()
        ));
    }

    @ApiOperation("获取下单地址列表")
    @GetMapping("/getListByUser")
    public Result<?> getListByUser(@RequestParam String addressId) {
        return Result.ok(hcSiteAddressDetailsService.getListByUser(addressId));
    }

    @ApiOperation("获取下单地址列表New")
    @GetMapping("/getListByUserNew")
    public Result<?> getListByUser(@RequestParam String addressId,@RequestParam String merchantIds) {
        if (StringUtils.isEmpty(merchantIds)){
           return Result.OK(new ArrayList<HcSiteAddressDetails>());
        }
        return Result.ok(hcSiteAddressDetailsService.getListByUserNew(addressId,merchantIds));
    }

    /**
     * 分页列表查询
     *
     * @param hcSiteAddressDetails
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "站点地址明细-分页列表查询")
    @ApiOperation(value = "站点地址明细-分页列表查询", notes = "站点地址明细-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<HcSiteAddressDetails>> queryPageList(HcSiteAddressDetails hcSiteAddressDetails,
                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                             HttpServletRequest req) {
        QueryWrapper<HcSiteAddressDetails> queryWrapper = QueryGenerator.initQueryWrapper(hcSiteAddressDetails, req.getParameterMap());
        Page<HcSiteAddressDetails> page = new Page<HcSiteAddressDetails>(pageNo, pageSize);
        IPage<HcSiteAddressDetails> pageList = hcSiteAddressDetailsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hcSiteAddressDetails
     * @return
     */
    @AutoLog(value = "站点地址明细-添加")
    @ApiOperation(value = "站点地址明细-添加", notes = "站点地址明细-添加")
    //@RequiresPermissions("org.jeecg.modules:hc_site_address_details:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody HcSiteAddressDetails hcSiteAddressDetails) {
        hcSiteAddressDetailsService.save(hcSiteAddressDetails);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hcSiteAddressDetails
     * @return
     */
    @AutoLog(value = "站点地址明细-编辑")
    @ApiOperation(value = "站点地址明细-编辑", notes = "站点地址明细-编辑")
    //@RequiresPermissions("org.jeecg.modules:hc_site_address_details:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody HcSiteAddressDetails hcSiteAddressDetails) {
        hcSiteAddressDetailsService.updateById(hcSiteAddressDetails);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "站点地址明细-通过id删除")
    @ApiOperation(value = "站点地址明细-通过id删除", notes = "站点地址明细-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:hc_site_address_details:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        hcSiteAddressDetailsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
//    @AutoLog(value = "站点地址明细-批量删除")
//    @ApiOperation(value = "站点地址明细-批量删除", notes = "站点地址明细-批量删除")
    //@RequiresPermissions("org.jeecg.modules:hc_site_address_details:deleteBatch")
//    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hcSiteAddressDetailsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "站点地址明细-通过id查询")
    @ApiOperation(value = "站点地址明细-通过id查询", notes = "站点地址明细-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<HcSiteAddressDetails> queryById(@RequestParam(name = "id", required = true) String id) {
        HcSiteAddressDetails hcSiteAddressDetails = hcSiteAddressDetailsService.getById(id);
        if (hcSiteAddressDetails == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hcSiteAddressDetails);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hcSiteAddressDetails
     */
    //@RequiresPermissions("org.jeecg.modules:hc_site_address_details:exportXls")
//    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HcSiteAddressDetails hcSiteAddressDetails) {
        return super.exportXls(request, hcSiteAddressDetails, HcSiteAddressDetails.class, "站点地址明细");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("hc_site_address_details:importExcel")
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HcSiteAddressDetails.class);
    }

}
